package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.AuthThreadLocal;
import com.itheima.common.JwtToken;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.mapper.OrderDetailsMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ShoppingCartMapper;

import com.itheima.pojo.dto.orderDto.OrderAddDto;
import com.itheima.pojo.dto.orderDto.OrderDetails;
import com.itheima.pojo.dto.orderDto.OrderUpdateDto;
import com.itheima.pojo.vo.addressBookVo.AddressBookByIdVo;

import com.itheima.pojo.vo.orderVo.GetOrderVo;
import com.itheima.pojo.vo.orderVo.OrderByPageVo;
import com.itheima.pojo.vo.shoppingCartVo.GetShoppingCartVo;
import com.itheima.service.OrderService;


import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tcotk
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper mapper;
    @Resource
    private OrderDetailsMapper orderDetailsMapper;
    @Resource
    private AddressBookMapper addressBookMapper;
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    public boolean update(OrderUpdateDto order) {

        int update = mapper.update(order);
        return update > 0;
    }

    @Override
    public List<GetOrderVo> selectAll() throws ParseException {
           String str="yyyy-MM-dd HH:mm:ss" ;
        List<GetOrderVo> list = mapper.selectAll();
        for (GetOrderVo order : list) {
            SimpleDateFormat sdf =new SimpleDateFormat(str);

            long time = sdf.parse(order.getCheckoutTime()).getTime();
            long time1 = System.currentTimeMillis();
            if (time1 >= time) {
                order.setStatus(1);

            } else {
                order.setStatus(0);

            }

        }
        return list;
    }

    @Override
    public List<OrderByPageVo> selectByPage(int page, int pageSize, String number, String endTime, String beginTime) throws ParseException {
        page = (page - 1) * pageSize;
        List<OrderByPageVo> list = mapper.selectByPage(page, pageSize, number, endTime, beginTime);
        for (OrderByPageVo order : list) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            long time = sdf.parse(order.getCheckoutTime()).getTime();
            long time1 = System.currentTimeMillis();
            if (time1 >= time) {
                order.setStatus(1);

            } else {
                order.setStatus(0);

            }

        }
        return list;
    }

    @Override
    public List<OrderByPageVo> selectByPage(int page, int pageSize) {
        return null;
    }

    @Override
    public boolean add(OrderAddDto order) {
        String token = AuthThreadLocal.getAmount();
        String userId = JwtToken.parse(token);
        String addressBookId = order.getAddressBookId();
        AddressBookByIdVo addressBook = addressBookMapper.selectById(addressBookId);
        order.setUserId(userId);
        order.setUserName(addressBook.getConsignee());
        order.setAddress(addressBook.getDetail());
        order.setAddressBookId(addressBook.getId());
        order.setPhone(addressBook.getPhone());
        order.setConsignee(addressBook.getConsignee());

        String str = IdUtil.getSnowflakeNextIdStr();


        order.setId(str);
        String number = "d" + System.currentTimeMillis();
        order.setNumber(number);
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(LocalDateTime.now().plusHours(1));
        List<GetShoppingCartVo> cartList = shoppingCartMapper.selectAll();
        BigDecimal amount = order.getAmount();

        int result1 = 0;
        OrderDetails orderDetail = new OrderDetails();
        List<OrderDetails> list = new ArrayList<>();

        for (GetShoppingCartVo cart : cartList) {
            String name = cart.getName();
            Integer num1 = cart.getNumber();
            BigDecimal num = new BigDecimal(Integer.parseInt(num1.toString()));
            BigDecimal cartAmount = cart.getAmount();

            amount = cartAmount.multiply(num);
            amount = amount.plus();

            orderDetail.setOrderId(order.getId());
            orderDetail.setName(name);
            orderDetail.setNumber(num1);
            list.add(orderDetail);
            result1 = orderDetailsMapper.add(orderDetail);
            result1 *= result1;
        }

        order.setAmount(amount);
        int result = orderMapper.add(order);
        return result * result1 > 0;
    }
}
